package com.fpd.tools;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSON;
import com.fpd.constant.Constants;
import com.fpd.model.bean.SmsResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

/**
 * @desc 短信接口工具类
 * @author wujiangbo
 * @date 2019年11月19日 上午11:42:04
 */
@Component
@Slf4j
public class SmsUtil {

    @Value("${yunpian.sendMessage.apiKey}")
    private String apiKey;
    
    @Value("${yunpian.sendMessage.signature}")
    private String signature;
    
    @Value("${yunpian.sendMessage.uri}")
    private String url;


    /**
     * @desc 发送短信验证码
     * @author wujiangbo
     * @date 2019年11月19日 下午1:43:54
     * @param mobile 手机号
     * @param code 验证码
     * @return SmsResult
     */
    public SmsResult sendCode(String mobile, String code) {
        Map<String, String> params = new HashMap<>();
        params.put("apikey", apiKey);
        String text = "【"+ signature +"】您的验证码是" + code;
        params.put("text", text);
        params.put("mobile", mobile);
        String result_str = post(url, params);
        SmsResult result = JSON.parseObject(result_str, SmsResult.class);
        return result;
    }
    
    /**
     * @desc 基于HttpClient 4.3的通用POST方法
     * @author wujiangbo
     * @date 2019年11月19日 上午11:47:59
     * @param url 提交的URL
     * @param paramsMap 提交<参数，值>Map
     * @return String
     */
    public static String post(String url, Map < String, String > paramsMap) {
        CloseableHttpClient client = HttpClients.createDefault();
        String responseText = "";
        CloseableHttpResponse response = null;
        try {
            HttpPost method = new HttpPost(url);
            if (paramsMap != null) {
                List < NameValuePair > paramList = new ArrayList <NameValuePair > ();
                for (Map.Entry < String, String > param: paramsMap.entrySet()) {
                    NameValuePair pair = new BasicNameValuePair(param.getKey(),
                        param.getValue());
                    paramList.add(pair);
                }
                method.setEntity(new UrlEncodedFormEntity(paramList, Constants.SYSTEM_ENCODING));
            }
            response = client.execute(method);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                responseText = EntityUtils.toString(entity, Constants.SYSTEM_ENCODING);
            }
        } catch (Exception e) {
        	log.error("SmsUtil -- post -- error:{}",e);
        } finally {
            try {
                response.close();
            } catch (Exception e) {
            	log.error("SmsUtil -- post -- response.close()  -- error:{}",e);
            }
        }
        return responseText;
    }
}
